﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Brain.Build;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;

namespace Brain.Editor
{
    public partial class ContentManagerForm : DockContent
    {
        public ContentManagerForm()
        {
            InitializeComponent();
        }

        private bool Accept;
        public ContentManagerForm(bool accept)
            : this()
        {
            this.Accept = accept;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            UpdateList();
        }

        private void tool_Add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Engine.Instance.SerializationManager.SceneData.RootFolder))
            {
                MessageBox.Show("You need to define a folder for your scene before trying to loading content, try hitting File->Save");
                return;
            }

            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = @"All files|*.*";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = fileDialog.FileName;
                string fileName = Path.GetFileName(file);

                string contentFolder = Path.Combine(Engine.Instance.SerializationManager.SceneData.RootFolder, "Content");
                string finalPlace = Path.Combine(contentFolder, fileName);

                if (!Directory.Exists(contentFolder))
                {
                    Directory.CreateDirectory(contentFolder);
                }
                if (!File.Exists(finalPlace))
                {
                    File.Copy(file, finalPlace);
                }
                SceneContentFile c = new SceneContentFile();
                c.FileExtension = Path.GetExtension(file);
                c.Name = Path.GetFileNameWithoutExtension(file);
                c.Project = Engine.Instance.SerializationManager.SceneData.ContentProject;
                c.RelativePath = Path.GetFileName(finalPlace);
                ContentFileUtil.InsertDefaultProcessors(c.FileExtension, out c.Processor, out c.Importer);

                ContentBuilder builder = new ContentBuilder();
                builder.Add(file, c.Name, c.Importer, c.Processor, null);
                string building = builder.Build();


                string TempFolder = Path.Combine(contentFolder, "Temp");
                if (!Directory.Exists(TempFolder))
                {
                    Directory.CreateDirectory(TempFolder);
                }

                if (string.IsNullOrEmpty(building))
                {
                    File.Copy(Path.Combine(builder.OutputDirectory, c.Name + ".xnb"),
                        Path.Combine(TempFolder, c.Name + ".xnb"), true);
                }
                c.ProcessedPath = Path.Combine(TempFolder, c.Name);

                builder.Dispose();

                if (string.IsNullOrEmpty(building))
                {
                    Engine.Instance.SerializationManager.SceneData.ContentProject.Files.Add(c);
                    UpdateList();
                }
                else
                {
                    MessageBox.Show("Couldn't compile your file!");
                }
            }
        }

        private void UpdateList()
        {
            this.listBox_Assets.Items.Clear();

            if (Engine.Instance != null &&
                Engine.Instance.SerializationManager.SceneData != null &&
                Engine.Instance.SerializationManager.SceneData.ContentProject != null)
            {
                for (int i = 0; i < Engine.Instance.SerializationManager.SceneData.ContentProject.Files.Count; i++)
                {
                    SceneContentFile file = Engine.Instance.SerializationManager.SceneData.ContentProject.Files[i];
                    this.listBox_Assets.Items.Add(file);
                }
            }
        }

        /// <summary>
        /// Previews the Selected content file
        /// </summary>
        private void PreviewContent(SceneContentFile file)
        {
            PreviewForm form = new PreviewForm(file);
            form.ShowDialog();
        }

        private void tool_Preview_Click(object sender, EventArgs e)
        {
            if (listBox_Assets.SelectedItem is SceneContentFile)
            {
                PreviewContent(listBox_Assets.SelectedItem as SceneContentFile);
            }
        }

        public SceneContentFile SelectedFile;
        private void listBox_Assets_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (Accept)
            {
                if (listBox_Assets.SelectedItem is SceneContentFile)
                {
                    SelectedFile = listBox_Assets.SelectedItem as SceneContentFile;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
        }
    }
}
